import java.sql.SQLOutput;
import java.util.*;

/*
* 使用集合实现斗地主游戏的部分功能，要求如下：

 （1）首先准备 54 张扑克牌并打乱顺序。

 （2）由三个玩家交替摸牌，每人 17 张扑克牌，最后三张留作底牌。

 （3）查看三个玩家手中的扑克牌和底牌。

 （4）其中玩家手中的扑克牌需要按照大小顺序打印，规则如下：

    手中扑克牌从大到小的摆放顺序：大王,小王,2,A,K,Q,J,10,9,8,7,6,5,4,3
* */
public class CardTest {

    public static void main(String[] args) {
        int index = 0;
        String str;
        //1.定义一个字符串数组，用于存储54张扑克牌
        String []c = new String[54];
        //1.1创建花色字符串数组
        String []color = {"红桃","黑桃","方片","梅花"};
        //1.2创建数字字符串数组
        String []number = new String[13];
        for (int i = 1; i <= number.length; i++) {
            if (i == 1){
                number[i-1] = "A";
            }
            else if (i == 11){
                number[i-1] = "J";
            }
            else if (i == 12){
                number[i-1] = "Q";
            }
            else if (i == 13){
                number[i-1] = "K";
            }
            else{
                number[i-1] = "" + i;
            }
        }
        //1.3组合花色和数字字符串,并赋值给card数组
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                c[index] = color[i] + number[j];
                index++;
            }
        }
        c[52] = "大王";
        c[53] = "小王";

        //2.将扑克牌数组传入集合中
        List<String> card = new LinkedList<String>();
        for (int i = 0; i < c.length; i++) {
            card.add(i,c[i]);
        }
        //3.定义一个Map集合用于存储扑克牌对应的大小
        Map<String,Integer> map = new HashMap<String,Integer>();
        //存储大小王
        map.put("大王",100);
        map.put("小王",99);
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                str = color[i] + number[j];
                if(number[j].equals("J")){
                    map.put(str,11);
                }
                else if (number[j].equals("Q")){
                    map.put(str,12);
                }
                else if (number[j].equals("K")){
                    map.put(str,13);
                }
                else if (number[j].equals("A")){
                    map.put(str,14);
                }
                else if (number[j].equals("2")){
                    map.put(str,15);
                }
                else {
                    map.put(str,Integer.parseInt(number[j]));
                }
            }
        }
        //4.打乱扑克牌顺序
        Collections.shuffle(card);

        //5.创建三个玩家和底牌的扑克牌集合，并让玩家抽牌
        /*String []player1 = new String[17];
        String []player2 = new String[17];
        String []player3 = new String[17];
        String []lowCard = new String[3];*/
        Map<String,Integer> player1 = new HashMap<String,Integer>();
        Map<String,Integer> player2 = new HashMap<String,Integer>();
        Map<String,Integer> player3 = new HashMap<String,Integer>();
        Map<String,Integer> lowCard = new HashMap<String,Integer>();

        for (int i = 0; i < 51; i+=3) {
            player1.put(card.get(i),map.get(card.get(i)));
            player2.put(card.get(i+1),map.get(card.get(i+1)));
            player3.put(card.get(i+2),map.get(card.get(i+2)));
        }
        for (int i = 51; i < 54; i++) {
            lowCard.put(card.get(i),map.get(i));
        }

        //6.给每个玩家手中的牌进行排序
        //将map转换为list
        List<Map.Entry<String, Integer>> user1 = new ArrayList<Map.Entry<String, Integer>>(player1.entrySet()); //转换为list
        List<Map.Entry<String, Integer>> user2 = new ArrayList<Map.Entry<String, Integer>>(player2.entrySet()); //转换为list
        List<Map.Entry<String, Integer>> user3 = new ArrayList<Map.Entry<String, Integer>>(player3.entrySet()); //转换为list
        //实现比较器
        /*Comparator<Map.Entry<String, Integer>> comparator = new Comparator<Map.Entry<String, Integer>>(){
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                return o2.getValue() - o1.getValue();
            }
        };*/
        //Lambda表达式
        Comparator<Map.Entry<String, Integer>> comparator = (o1, o2) -> {return o2.getValue() - o1.getValue();};
        //实现比较器的排序
        Collections.sort(user1, comparator);
        Collections.sort(user2, comparator);
        Collections.sort(user3, comparator);


        //7.打印结果
        System.out.println("玩家1手中的扑克牌为：");
        for (Map.Entry<String,Integer> m:user1) {
            System.out.print(m.getKey() + "   ");
        }
        System.out.println();
        System.out.println("玩家2手中的扑克牌为：");
        for (Map.Entry<String,Integer> m:user2) {
            System.out.print(m.getKey() + "   ");
        }
        System.out.println();
        System.out.println("玩家3手中的扑克牌为：");
        for (Map.Entry<String,Integer> m:user3) {
            System.out.print(m.getKey() + "   ");
        }
        System.out.println();
        System.out.println("底牌为：");
        for (Map.Entry<String,Integer> m:lowCard.entrySet()) {
            System.out.print(m.getKey() + "   ");
        }
    }
}
